package fr.ul.ade.secunde.ui.phone;

import android.content.Intent;

import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import fr.ul.ade.R;
import fr.ul.ade.secunde.util.Bundles;

/**
 * Classe responsable pour la gestion (création, gestion des évènements,
 * destruction) de {@link ActionMode} utilisé lors de l'affichage des détails
 * d'un emploi du temps.
 * 
 * @author Michaël Schnell
 * @since Jul 2, 2013
 * 
 * @version 1.5
 */
public class ScheduleDetailsActionMode
		implements ActionMode.Callback {

	private long scheduleId;
	private String scheduleName;

	private ScheduleList scheduleList;

	public ScheduleDetailsActionMode(ScheduleList scheduleList,
			long scheduleId, String scheduleName) {
		this.scheduleList = scheduleList;
		this.scheduleId = scheduleId;
		this.scheduleName = scheduleName;
	}

	@Override
	public boolean onCreateActionMode(ActionMode mode, Menu menu) {
		mode.getMenuInflater().inflate(R.menu.schedule_details, menu);
		return true;
	}

	@Override
	public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
		return true;
	}

	@Override
	public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_sync_schedule:
			Intent updateIntent = new Intent(this.scheduleList.getResources()
					.getString(R.string.intent_action_update_schedule));
			updateIntent.putExtra(Bundles.SCHEDULE_ID, this.scheduleId);
			this.scheduleList.startService(updateIntent);
			return true;

		case R.id.action_edit:
			this.scheduleList.showEditForm(this.scheduleId);
			return true;

		case R.id.action_delete:
			this.scheduleList
					.showDeleteForm(this.scheduleId, this.scheduleName);
			return true;

		default:
		}

		return false;
	}

	@Override
	public void onDestroyActionMode(ActionMode mode) {
		this.scheduleList.onBackPressed();
	}

}// class ScheduleDetailsActionMode